package com.example.demo.controller;

import com.example.demo.model.UserInfo;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/reg")
    public HashMap<String,Object> reg(String username,String password){
        HashMap<String,Object> result=new HashMap<>();
        String msg="注册失败";
        int succ=200;
        int state=-1;
        if (username!= null && password!= null &&
                !"".equals( username) && !"".equals(password)) {
            state=userService.add3(username,password);
        }else {
            msg="信息异常";
            state=-2;
        }

        result.put("succ",succ);
        result.put("state",state);
        result.put("msg",msg);
        return result;
    }




    @RequestMapping("/getall2")
    public List<UserInfo> getAll2(String order){
        return userService.getAll2(order);
    }


    @RequestMapping("/getall")
    public Object java(){
        return userService.getAll();
    }
    @RequestMapping("/findbyname")
    public List<UserInfo> findByName(String name){
        if (name==null||"".equals(name)){
            return null;
        }
        return userService.findByName(name);
    }
    @RequestMapping("/update")
    public int updatePassword(Integer id, String password) {
        // 参数效验
        if (id <= 0 || password == null || password.equals("")
        ) { // 非法参数
            return 0;
        }
        return userService.updatePassword(id, password);
    }
    @RequestMapping("/add")
    public int add(UserInfo userInfo){
        if (userInfo==null||userInfo.getUsername()==null
                ||"".equals(userInfo.getUsername())
                ||userInfo.getPassword()==null
                ||"".equals(userInfo.getPassword())){
            return -1;
        }
        return userService.add(userInfo);
    }
    @RequestMapping("/del")
    public int del(int id){
        if (id<=0){
            return 0;
        }
        return userService.del(id);
    }
    @RequestMapping("/add2")
    public int add2(UserInfo userInfo){
        if (userInfo==null||userInfo.getUsername()==null
                ||"".equals(userInfo.getUsername())
                ||userInfo.getPassword()==null
                ||"".equals(userInfo.getPassword())){
            return -1;
        }
        userService.add2(userInfo);
        return userInfo.getId();
    }



    @RequestMapping("/login")
    public Object login(String username, String password, HttpServletRequest request) {
        HashMap<String, Object> result = new HashMap<>();
        result.put("succ", 200); // 后端响应状态码
        String msg = ""; //
        int state = -1; // 登录状态码
        if (username!= null && password!= null &&
               !"".equals( username) && !"".equals(password)) {
            // 登录成功
            UserInfo userInfo=userService.login(username,password);
            if (userInfo!=null){
                state = 1;
                msg=userInfo.toString();
                System.out.println("登录成功"+userInfo);
            }else {
                msg="用户名或密码输入错误，请先检查你的输入！";
            }
        }
        else {
            // 登录失败
            msg = "用户名或密码格式错误，请先检查！";
        }
        result.put("state", state);
        result.put("msg", msg);
        return result;
    }
}
